// 收集所有函数的调用参数
const babel = require('@babel/core');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;

// 要转换的代码
const code = `
function greet(name) {
    console.log("Hello, " + name);
}
greet("World");
greet("Alice");
`;

// 解析代码为 AST
const ast = parser.parse(code);

// 定义 visitor 对象
const visitor = {
    CallExpression(path) {
        const args = path.node.arguments.map(arg => arg.value);
        console.log(`Function called with arguments: ${args.join(', ')}`);
    }
};

// 遍历 AST
traverse(ast, visitor);
// Function called with arguments: World
// Function called with arguments: Alice




// 将所有的console.log 替换成自定义的日志函数

// 要转换的代码
const code1 = `
console.log("Hello, World!");
console.log("This is a test.");
`;

// 解析代码为 AST
const ast1 = parser.parse(code);

// 定义 visitor 对象
const visitor1 = {
    CallExpression(path) {
        if (path.node.callee.object.name === 'console' && path.node.callee.property.name === 'log') {
            const customLogCall = {
                type: 'CallExpression',
                callee: {
                    type: 'Identifier',
                    name: 'customLog'
                },
                arguments: path.node.arguments
            };
            path.replaceWith(customLogCall);
        }
    }
};

// 遍历 AST
traverse(ast, visitor);

// 将修改后的 AST 转换回代码
const { code: transformedCode } = generate(ast);

// 输出转换后的代码
console.log(transformedCode);


// customLog("Hello, World!");
// customLog("This is a test.");



/**
 * 收集所有的 console 调用并记录
 */

// 要转换的代码
const code2 = `
console.log("Hello, World!");
console.warn("Warning!");
console.error("Error occurred");
`;

// 解析代码为 AST
const ast2 = parser.parse(code2);

// 定义 visitor 对象
const consoleCalls = [];
const visitor2 = {
    CallExpression(path) {
        if (path.node.callee.object && path.node.callee.object.name === 'console') {
            consoleCalls.push({
                method: path.node.callee.property.name,
                args: path.node.arguments.map(arg => arg.value)
            });
        }
    }
};

// 遍历 AST
traverse(ast2, visitor2);

// 输出收集到的 console 调用
console.log('Collected console calls:');
consoleCalls.forEach(call => {
    console.log(`Method: ${call.method}, Args: ${call.args.join(', ')}`);
});
// 输出
// Collected console calls:
// Method: log, Args: Hello, World!
// Method: warn, Args: Warning!
// Method: error, Args: Error occurred









/**
 * 抽取重复的逻辑变为函数
 */

// 要转换的代码
const code3 = `
function test() {
    console.log("Hello");
    console.log("Hello");
}
`;

// 解析代码为 AST
const ast3 = parser.parse(code);

// 定义 visitor 对象
let extractedFunction = null;
const visitor3 = {
    CallExpression(path) {
        if (path.node.callee.object && path.node.callee.object.name === 'console' &&
            path.node.callee.property.name === 'log' &&
            path.node.arguments[0].value === "Hello") {
            if (!extractedFunction) {
                extractedFunction = {
                    type: 'FunctionDeclaration',
                    id: { type: 'Identifier', name: 'logHello' },
                    params: [],
                    body: {
                        type: 'BlockStatement',
                        body: [
                            {
                                type: 'ExpressionStatement',
                                expression: {
                                    type: 'CallExpression',
                                    callee: {
                                        type: 'MemberExpression',
                                        object: { type: 'Identifier', name: 'console' },
                                        property: { type: 'Identifier', name: 'log' }
                                    },
                                    arguments: [{ type: 'StringLiteral', value: 'Hello' }]
                                }
                            }
                        ]
                    }
                };
            }
            path.remove(); // 删除原有的 console.log
        }
    }
};

// 遍历 AST
traverse(ast, visitor);

// 如果提取了函数，则将其添加到 AST
if (extractedFunction) {
    ast.program.body.unshift(extractedFunction);
}

// 将修改后的 AST 转换回代码
const { code: transformedCode3 } = generate(ast);

// 输出转换后的代码
console.log(transformedCode3);
// 输出
// function logHello() {
//     console.log("Hello");
// }

// function test() {
//     logHello();
//     logHello();
// }